package com.hskn.hss.module.userrole.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hskn.hss.entity.AjaxResult;
import com.hskn.hss.exception.HssException;
import com.hskn.hss.module.userrole.entity.SysUserRole;
import com.hskn.hss.module.userrole.mapper.SysUserRoleMapper;
import com.hskn.hss.module.userrole.service.ISysUserRoleService;
import com.hskn.hss.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 用户角色表 服务实现类
 * </p>
 *
 * @author hskn
 * @since 2021-04-29
 */
@Service
public class SysUserRoleServiceImpl extends ServiceImpl<SysUserRoleMapper, SysUserRole> implements ISysUserRoleService {

    @Autowired
    SysUserRoleMapper sysUserRoleMapper;

    @Override
    public AjaxResult insert(JSONObject params) throws HssException {
        SysUserRole sysUserRole = params.getObject("data", SysUserRole.class);
        sysUserRole.setId(StringUtils.uuid());
        int i = sysUserRoleMapper.insert(sysUserRole);
        boolean flag = i > 0 ? true : false;

        if (flag) {
            return AjaxResult.success("操作成功");
        } else {
            return AjaxResult.fail("操作失败，请稍后再试");
        }
    }

    @Override
    public AjaxResult deleteByid(JSONObject params) throws HssException {
        String id = params.getString("id");
        int i = sysUserRoleMapper.deleteById(id);
        boolean flag = i > 0 ? true : false;
        if (flag) {
            return AjaxResult.success("操作成功");
        } else {
            return AjaxResult.fail("操作失败，请稍后再试");
        }
    }

    @Override
    public void deleteByUid(String uid) throws HssException {
        Map map = new HashMap();
        map.put("userid", uid);
        sysUserRoleMapper.deleteByMap(map);
    }

    @Override
    public AjaxResult update(JSONObject params) throws HssException {
        SysUserRole sysUserRole = params.getObject("data", SysUserRole.class);
        int i = sysUserRoleMapper.updateById(sysUserRole);
        boolean flag = i > 0 ? true : false;
        if (flag) {
            return AjaxResult.success("操作成功");
        } else {
            return AjaxResult.fail("操作失败，请稍后再试");
        }
    }

    @Override
    public AjaxResult updateByUserid(String uid, String[] rids) {
        Map map = new HashMap();
        map.put("userid", uid);
        try {
            sysUserRoleMapper.deleteByMap(map);
            for (String rid : rids) {
                SysUserRole sysUserRole = new SysUserRole();
                sysUserRole.setId(StringUtils.uuid());
                sysUserRole.setRoleid(rid);
                sysUserRole.setUserid(uid);
                sysUserRoleMapper.insert(sysUserRole);
            }
            return AjaxResult.success("操作成功");
        } catch (Exception e) {
            e.printStackTrace();
            log.error("报错ERROR", e);
            return AjaxResult.fail("操作失败，请稍后再试");
        }


    }

    @Override
    public SysUserRole getInfoById(JSONObject params) throws HssException {
        String id = params.getString("id");
        SysUserRole sysUserRole = sysUserRoleMapper.selectById(id);
        return sysUserRole;
    }

    @Override
    public List<SysUserRole> getListByMap(JSONObject params) throws HssException {
        Map map = new HashMap();
        map = params.getObject("queryparams", Map.class);
        List<SysUserRole> userRolesList = sysUserRoleMapper.selectByMap(map);
        return userRolesList;
    }
    @Override
    public List<SysUserRole> getSysUserRoleListByRoleId(String roleId) {
        if (StringUtils.isEmpty(roleId)){
            return new ArrayList<SysUserRole>();
        }
        QueryWrapper<SysUserRole> userRoleQueryWrapper = new QueryWrapper<SysUserRole>();
        userRoleQueryWrapper.lambda().eq(SysUserRole::getRoleid,roleId);
        List<SysUserRole> sysUserRoleList = sysUserRoleMapper.selectList(userRoleQueryWrapper);
        return sysUserRoleList;
    }
}
